package com.fare.taluo.application.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.core.domain.config.channel.AirCompanyEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.AirCompanyEntityVO;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.SourceVO;
import com.fare.taluo.application.PageUtilApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;

@Transactional
@Scope("singleton")
@Service("pageUtilApplication")
public class PageUtilApplicationImpl implements PageUtilApplication{
	
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;

	@Override
	public List<SourceVO> channels() {
		List<SourceEntityVO> channels = sourceApplication.findAllSourceEntity();
		List<SourceVO> resultList = new ArrayList<SourceVO>();
		for(int i = 0; i < channels.size(); i++){
			SourceVO vo = new SourceVO(channels.get(i));
			resultList.add(vo);
		}
		return resultList;
	}
	@Override
	public List<SourceVO> channelsNotNanHang() {
		List<SourceEntityVO> channels = sourceApplication.findAllSourceEntity();
		List<SourceVO> resultList = new ArrayList<SourceVO>();
		for(int i = 0; i < channels.size(); i++){
			SourceVO vo = new SourceVO(channels.get(i));
			//过滤“中国南方航空官网”
			if("中国南方航空官网".equals(vo.getChannel())){
				continue;
			}
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public String toJSON(List<? extends Object> vos) throws Exception {
		String jsonStr = "{\"Total\":"+vos.size()+","+
				 "\"Rows\":";
		int i = 0;
		String json;
		json = JsonUtil.toJson(vos);
		jsonStr += json;
		jsonStr += "}";	
		return jsonStr;
	}

	@Override
	public List<FlightInfoVO> toFlightOfLine(String fromCityCode,
			String toCityCode) {
		FlightInfoEntity flight = new FlightInfoEntity();
		List<FlightInfoEntity> flights = flight.findByLine(fromCityCode, toCityCode);
		List<FlightInfoVO> vos = new ArrayList<FlightInfoVO>();
		for(int i = 0; i < flights.size(); i++){
			flight = flights.get(i);
			FlightInfoVO vo = new FlightInfoVO();
			vo.setFlightNo(flight.getFlightNo());
			vo.setFromCity(flight.getFromCity());
			vo.setToCity(flight.getToCity());
			vo.setFromCityName(flight.getFromCityName());
			vo.setToCityName(flight.getToCityName());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public List<AirCompanyEntityVO> carriers() {
		AirCompanyEntity company = new AirCompanyEntity();
		List<AirCompanyEntity> companies = company.all();
		List<AirCompanyEntityVO> vos = new ArrayList<AirCompanyEntityVO>();
		for(int i = 0; i < companies.size(); i++){
			AirCompanyEntityVO vo = new AirCompanyEntityVO();
			vo.setName(companies.get(i).getName());
			vo.setId(companies.get(i).getId());
			vos.add(vo);
		}
		return vos;
	}

}
